package com.team622.shop.marketing.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.team622.shop.common.PageResult;
import com.team622.shop.common.Result;
import com.team622.shop.marketing.model.entity.ActivityRule;
import com.team622.shop.marketing.service.service.ActivityRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author team622
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/activityRule")
@Tag(name = "活动规则管理", description = "活动规则的CRUD操作")
public class ActivityRuleController {

    @Autowired
    private ActivityRuleService activityRuleService;

    @PostMapping
    @Operation(summary = "新增活动规则", description = "创建一条新的活动规则")
    public Result<ActivityRule> addActivityRule(@RequestBody ActivityRule activityRule) {
        ActivityRule savedActivityRule = activityRuleService.addActivityRule(activityRule);
        return Result.success(savedActivityRule);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除活动规则", description = "根据ID删除活动规则")
    public Result<Boolean> deleteActivityRule(@Parameter(description = "活动规则ID") @PathVariable("id") Long id) {
        boolean result = activityRuleService.deleteActivityRule(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新活动规则", description = "更新活动规则信息")
    public Result<ActivityRule> updateActivityRule(@RequestBody ActivityRule activityRule) {
        ActivityRule updatedActivityRule = activityRuleService.updateActivityRule(activityRule);
        return Result.success(updatedActivityRule);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取活动规则详情", description = "根据ID获取活动规则详情")
    public Result<ActivityRule> getActivityRuleById(@Parameter(description = "活动规则ID") @PathVariable("id") Long id) {
        ActivityRule activityRule = activityRuleService.getActivityRuleById(id);
        return Result.success(activityRule);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询活动规则", description = "分页查询活动规则，支持多条件查询")
    public PageResult<ActivityRule> pageActivityRule(
            @RequestParam(value = "current", defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(value = "pageSize", defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(value = "activityId", required = false) @Parameter(description = "活动ID") Long activityId,
            @RequestParam(value = "ruleType", required = false) @Parameter(description = "规则类型") Integer ruleType
    ) {
        IPage<ActivityRule> page = activityRuleService.pageActivityRule(
                current, pageSize, activityId, ruleType);
        return PageResult.success(page);
    }
}

